package com.sfx.test;

import com.sfx.jedis.util.JedisConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-19
 * Time: 14:28
 */
public class JedisTestDemo {
    private Jedis jedis;

    @BeforeEach
    void setUp() {
        //1.建立连接(ip+端口)
        //jedis = new Jedis("101.42.177.102",6379);
        //不用直接创建,直接去Jedis连接池去拿,不用的时候就放回了连接池
        jedis = JedisConnectionFactory.getJedis();
        //2.设置密码
        jedis.auth("123321");
        //3.设置redis库(默认是0号库)
        jedis.select(0);
    }

    @Test
    void testString() {
        //操作Redis测试String类型
        String res = jedis.set("name", "黑犀铠甲");
        System.out.println("操作是否成功 : " + res);
        String getName = jedis.get("name");
        System.out.println("name : " + getName);
    }

    @AfterEach
    void tearDown() {
        //释放连接
        if(jedis!=null){
            jedis.close();
        }
    }
}